package conf2.ui;

import conf2.model.EActor;
import conf2.model.EActor.Side;
import java.awt.Point;
import java.util.Collection;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.Map;

/**
 *
 * @author Administrator
 */
public class PieceCollection implements Iterable<GamePiece>
{

    private Map<EActor, GamePiece> pieces = new EnumMap<EActor, GamePiece>(EActor.class);

    public PieceCollection(Collection<GamePiece> createFrom)
    {
        for (GamePiece gp : createFrom) {
            pieces.put(gp.getActor(), gp);
        }
    }

    /**
     * Get the first piece encountered at a certain location.
     * @param where where to look.
     * @param disregard piece to disregard, even if it is at the given location.
     * May be null.
     * @return a piece instance, or null if no piece was at the location.
     */
    public GamePiece getPieceAt(Point where, GamePiece disregard)
    {
        for (GamePiece gp : pieces.values()) {
            if (gp.getBounds().contains(where) && gp != disregard) {
                return gp;
            }
        }
        return null;
    }

    public Iterator<GamePiece> iterator()
    {
        return pieces.values().iterator();
    }

    public GamePiece get(EActor ea)
    {
        return pieces.get(ea);
    }

    public Collection<GamePiece> values()
    {
        return pieces.values();
    }

    public void setSideMovable(Side side, boolean movable)
    {
        for (GamePiece gp : pieces.values()) {
            if (gp.getActor().getSide() == side) {
                gp.setMovable(movable);
            }
        }
    }

    public void setSideVisible(Side side, boolean visible)
    {
        for (GamePiece gp : pieces.values()) {
            if (gp.getActor().getSide() == side) {
                gp.setVisible(visible);
            }
        }
    }

    public void setSideKnown(Side side, boolean known)
    {
        for (GamePiece gp : pieces.values()) {
            if (gp.getActor().getSide() == side) {
                gp.setKnown(known);
            }
        }
    }
}
